package com.yvan.js;

import jdk.nashorn.api.scripting.ScriptObjectMirror;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;

import javax.script.Bindings;
import javax.script.SimpleBindings;

@Slf4j
public class Module extends SimpleBindings {
    // 加载Module时定义的 module 对象
    @Getter
    private Bindings module;
    // // 当前Module依赖(require)的 module 对象
    // @Getter
    // private List<Bindings> children = new ArrayList<>();
    // JS Module 定义的导出对象
    @Getter
    @Setter
    private ScriptObjectMirror exports;

    public Module(String id, Bindings module, ScriptObjectMirror exports) {
        this.module = module;
        this.exports = exports;
        // this.module.put("children", children);
        this.module.put("exports", this.exports);
        this.module.put("id", id);
        this.module.put("loaded", false);
        this.module.put("console", new Console(id));
    }

    // 设置加载成功
    void setLoaded() {
        module.put("loaded", true);
    }
}
